package com.axun.ckz.yytmanager.base;

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.axun.ckz.yytmanager.R;

/**
 * Created by ckz on 2018/4/23.
 */

public class BaseActivity extends AppCompatActivity implements IBaseView{

    private Context context;
    private AlertDialog dialog;
    private View contentView;
    private TextView tvMsg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        context = this;
        dialog = new AlertDialog.Builder(this, R.style.LoadDialog).create();
        dialog.setCancelable(false);
        contentView = LayoutInflater.from(this).inflate(R.layout.dialog_progress,null);
        tvMsg = contentView.findViewById(R.id.tv_msg);
    }

    public Context getContext(){
        return context;
    }

    public void showToast(String toast){
        Toast.makeText(context,toast,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showProgress(final String progress) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               if (dialog!= null && !dialog.isShowing()){
                   dialog.show();
                   dialog.setContentView(contentView);
                   if (!TextUtils.isEmpty(progress)){
                       tvMsg.setVisibility(View.VISIBLE);
                       tvMsg.setText(progress);
                   }else {
                       tvMsg.setVisibility(View.GONE);
                   }
               }
           }
       });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (dialog!=null&&dialog.isShowing()){
                    dialog.dismiss();
                }
            }
        });
    }

    @Override
    public void showUI(String json) {

    }

    @Override
    public void showError(String error) {
        showToast(error);
    }

    @Override
    public void showFaild(String msg) {
        showToast(msg);
    }

    private void createLoadingDialog(){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog!=null){
            dialog.dismiss();
        }
    }
}
